package com.demo.service.impl.login;

import com.demo.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

///登录业务 处理
@Service
public class LoginServiceImpl {
    @Autowired
    JavaMailutil javaMailutil;
    @Autowired
    JavaPhone javaPhone;
    @Autowired
    LoginServiceImpl loginService;
    @Autowired
    private com.demo.service.impl.Service service;


    //生成6位随机验证码
    public  int yanzhengma() {
        int n = 0;
        while (n < 100000) {
            n = (int) (Math.random() * 1000000);
        }
        return n;
    }


    //测试手机号是否合法
    public    boolean isMobileNumber(String number) {
        if (StringUtils.isEmpty(number)) {
            return false;
        }
        if (11 != number.length()) {
            return false;
        }
        /**
         * 移动号段正则表达式
         */
        String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        String pat2 = "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        String pat3 = "^((133)|(153)|(177)|(18[0,1,9])|(149))\\d{8}$";
        /**
         * 虚拟运营商正则表达式
         */
        String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";

        Pattern pattern1 = Pattern.compile(pat1);
        Matcher match1 = pattern1.matcher(number);
        boolean isMatch1 = match1.matches();
        if (isMatch1) {
            return true;
        }
        Pattern pattern2 = Pattern.compile(pat2);
        Matcher match2 = pattern2.matcher(number);
        boolean isMatch2 = match2.matches();
        if (isMatch2) {
            return true;
        }
        Pattern pattern3 = Pattern.compile(pat3);
        Matcher match3 = pattern3.matcher(number);
        boolean isMatch3 = match3.matches();
        if (isMatch3) {
            return true;
        }
        Pattern pattern4 = Pattern.compile(pat4);
        Matcher match4 = pattern4.matcher(number);
        boolean isMatch4 = match4.matches();
        if (isMatch4) {
            return true;
        }
        return false;
    }
    //查看邮箱是否合法
    public  boolean isValidEmail(String email) {
        if ((email != null) && (!email.isEmpty())) {
            return Pattern.matches("^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", email);
        }
        return false;
    }
    //发送 验证码
    public void code(String shu){
        //生成验证码方法 生成一个6位数的验证码 赋值给 code
        int code=loginService.yanzhengma();
        if(isMobileNumber(shu)){
            //是手机号
            //发送手机短信
            javaPhone.phone(shu,code);
            redisCode(shu,code);
        }else if(isValidEmail(shu)){
            //是邮箱
            //发送邮箱
            javaMailutil.code(code,shu);
            //将邮箱 验证码加入redis
            redisCode(shu,code);
        }else{
            System.out.println("既不是手机号,也不是邮箱");
        }
    }
    //登录
    public  User login(String shu, int verificationcode){
        User user = new User();
        System.out.println("传进来的手机号(邮箱)" + shu + "传进来的验证码" + verificationcode);
        Jedis jedis = new Jedis("localhost");
        //shu1 存放从redis中查到的验证码
        int shu1 = Integer.parseInt(jedis.get(shu));
        System.out.println("redis中查到的验证码" +shu1);
        if (verificationcode == shu1) {
            //验证码正确
            if (service.yes(shu)) {
                System.out.println("验证码正确,且账号已注册");
                user=service.allMsg(shu);
                user.setToken("1231231");
                return user;
            } else {
                System.out.println("验证码错误,但账号未注册");
                user.setCode(3);
                return user;
            }
        } else {
            //验证码错误
            System.out.println("验证码错误");
            user.setCode(2);
            return user;
        }
    }
    public  void redisCode(String shu,int code){
        //连接本地的 Redis 服务 将邮箱,验证码加入 Redis
        Jedis jedis = new Jedis("localhost");
        //加入redis (手机号(邮箱),验证码)   手机号(邮箱):键   验证码:值
        jedis.set(shu, String.valueOf(code));
        System.out.println("传进来的邮箱是"+shu+"  验证码是"+code);
        System.out.println("redis里的验证码是:"+jedis.get(shu));
        System.out.println("redis里面的所有键 "+jedis.keys("*"));
        //设置过期时间为900秒,15分
        System.out.println("设置键的过期时间为900s:"+jedis.expire(shu, 900));
    }








}
